package com.zito.health.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zito.health.common.annotations.ResponseResult;
import com.zito.health.common.result.PageResult;
import com.zito.health.common.result.VerifyResult;
import com.zito.health.entity.Department;
import com.zito.health.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 部门管理
 * @author: Sam
 * @time: 2020/03/12 14:23
 */
@Slf4j
@ResponseResult
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PostMapping
    public VerifyResult save(Department department){
        VerifyResult verifyResult = new VerifyResult();
        departmentService.save(department);
        return verifyResult;
    }

    @PutMapping
    public VerifyResult update(Department department){
        VerifyResult verifyResult = new VerifyResult();
        departmentService.update(department);
        return verifyResult;
    }

    @GetMapping
    public PageResult list(Department department, @RequestParam(defaultValue = "0") Integer pageNo, @RequestParam(defaultValue = "10")Integer pageSize){

        PageHelper.startPage(pageNo,pageSize);
        List<Department> list = departmentService.query(department);
        PageInfo<Department> pageInfo = new PageInfo(list);
        PageResult<Department> result = new PageResult();
        result.setRows(pageInfo.getList());
        result.setTotal(pageInfo.getTotal());
        return result;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public VerifyResult delete(@PathVariable Integer id){
        VerifyResult verifyResult = new VerifyResult();
        departmentService.delete(id);
        return verifyResult;
    }
    @GetMapping("/{id}")
    public Department queryById(@PathVariable Integer id){
        Department department = departmentService.queryById(id);
        return department;
    }


}
